package com.teamwork.module03.controller.file;



import com.teamwork.common.result.Result;
import com.teamwork.module03.service.UserIconService;
import com.teamwork.module03.service.file.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.SystemProperties;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/upload")
@Slf4j
public class UploadController {

    @Value("${upload.basePath}")
    private String basePath;

    @Resource
    private UploadService uploadService;

    /**
     * 这里有个坑，如果input标签里的name属性和MulitpartFile 参数 名称不一致，
     * 会报空指针异常
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/mod02")
    public Result upload(MultipartFile file, HttpServletRequest request) throws IOException {


        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //获取当前时间
        String nowtime = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
        //获取文件后缀名 例如.png
        String endName = "." + originalFilename.split("\\.")[1];


        //从线程里获取用户id
        String userid = "21312";
        //加上模块前缀
        String prefix = "mod2";
        String finalName = prefix + "-" + userid + "-" + nowtime + endName;
        log.info("用户xxx上传了文件："+finalName+" --至"+basePath);

        //resources目录
       //String basePah = ResourceUtils.getURL("classpath:").getPath()+"img";
       //自定义目录
        //String basePah ="E:/imgs";


        //创建文件架
        File dir = new File(basePath);
        if(!dir.exists()){
            //不存在
            dir.mkdirs();
        }
        //获取文件对象
        File newFie =new File(basePath,finalName);
        //上传

       file.transferTo(newFie);
//        InputStream in =new FileInputStream()
//        OutputStream out =new FileOutputStream(newFie);
//        int a;
//        while ((a=in.read())!=-1){
//            out.write(a);
//        }
//        in.close();
//        out.close();


        //  String resPath =basePath+finalName;

            return Result.success("文件上传成功！",finalName);




    }


   //用户上传头像
    @PostMapping("/usericon/{userid}")
    public Result uploadUserIcon(@PathVariable("userid")Integer userid, MultipartFile file) throws IOException {

        log.info("userid:{},file{}",userid,file);
//        MultipartFile  file = (MultipartFile) map.get("file");
//        Integer userid = (Integer) map.get("userid");

        return uploadService.uploadUserIcon(file,userid);

    }
//    //发布文章封面
//    @PostMapping("/cover/{userid}")
//    public Result uploadCover(@PathVariable("userid")Integer userid,MultipartFile file) throws IOException {
//        log.info("userid:{},file{}",userid,file);
//        return uploadService.uploadCover(userid,file);
//    }
    //quill编辑器图片上传
    @PostMapping("/contentimg")
    public Result uploadConetntImg(MultipartFile file) throws IOException {
        return uploadService.uploadContentImg(file);
    }
}